package com.dmap.admin.service.backgroundv1.impl;

import com.dmap.admin.controller.background.param.PermissionParam;
import com.dmap.admin.dao.AdminPermissionsMapper;
import com.dmap.admin.po.AdminPermissions;
import com.dmap.admin.service.backgroundv1.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:46
 * \* Description:
 * \
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private AdminPermissionsMapper adminPermissionsMapper;

    @Override
    public AdminPermissions selectById(Long id) {
        return adminPermissionsMapper.selectByPrimaryKey(id);
    }

    @Override
    public AdminPermissions insert(PermissionParam param) {
        if (param == null) {
            return null;
        }
        AdminPermissions adminPermissions = new AdminPermissions();
        adminPermissions.setName(param.getName());
        adminPermissions.setDescription(param.getDescription());
        if (param.getParentId() != null) {
            adminPermissions.setParentId(Long.valueOf(param.getParentId()));
        }
        adminPermissions.setUrl(param.getUrl());
        adminPermissions.setHasChild(param.getHasChild());
        adminPermissions.setCreatetime(System.currentTimeMillis());
        adminPermissions.setModifyTime(adminPermissions.getCreatetime());
        int success = adminPermissionsMapper.insertSelective(adminPermissions);
        return (success == 1) ? adminPermissions : null;
    }

    @Override
    public AdminPermissions update(PermissionParam param) {
        if (param == null) {
            return null;
        }
        AdminPermissions adminPermissions = new AdminPermissions();
        adminPermissions.setId(Long.valueOf(param.getId()));
        adminPermissions.setName(param.getName());
        adminPermissions.setDescription(param.getDescription());
        if (param.getParentId() != null) {
            adminPermissions.setParentId(Long.valueOf(param.getParentId()));
        }
        adminPermissions.setUrl(param.getUrl());
        adminPermissions.setHasChild(param.getHasChild());
        adminPermissions.setModifyTime(System.currentTimeMillis());
        int success = adminPermissionsMapper.updateByPrimaryKeySelective(adminPermissions);
        return (success == 1) ? adminPermissions : null;
    }

    @Override
    public boolean delete(Long id) {
        int success = adminPermissionsMapper.deleteByPrimaryKey(id);
        return success == 1;
    }

    @Override
    public List<AdminPermissions> selectAllPermission() {
        return adminPermissionsMapper.selectByExample(null);
    }

}